/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketmanager;

/**
 *
 * @author Viktor
 */
import java.util.ArrayList;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;



public class GoogleTranslator {

 /**
  * Translates list of string
  * @param input
  * List of string with source text to be translated
  * @return
  * Same size array list with translated text
  * @throws Exception
  */
 public ArrayList<String> translate(ArrayList<String> input) throws Exception {


    // dunno why we need this
    Translate.setHttpReferrer("http://google.lv");

    // variable init
    ArrayList<String> resultSet = new ArrayList<String>();
    int border = 300;


    // every record translation cycle
    for (String sout :input) {
     // check for not null
   if (!(sout == null)){
       // used api cant translate more than 300-400 characters at once, check for lenght and furher
       //splitting of string for tranlation
       //issue: can cut words in middle

        if (sout.length() <= border) {
            resultSet.add( Translate.execute(sout, Language.ENGLISH, Language.LATVIAN));
        } else {
            int offset = 0;
            String result = "";

            while (offset + border < sout.length()) {
                result += (Translate.execute(sout.substring(offset,offset + border), Language.ENGLISH, Language.LATVIAN));
                offset += border;
            }
            result += (Translate.execute(sout.substring(offset,sout.length()), Language.ENGLISH, Language.LATVIAN));
            resultSet.add(result);
        }
    } else {
        resultSet.add("");
    }

    }
    return resultSet;
}
 /**
  * translates single String from Greek to English
  * @param sout
  * input string to be translated
  * @return
  * translated string
  * @throws Exception
  */
 public String translate(String source) throws Exception {



    Translate.setHttpReferrer("http://google.lv");
    String result = "";
    int border = 300;

     //   Translator translator = new Translator();
   if (!(source == null)){
        if (source.length() <= border)
            {
  //         Translate.execute(sout, Language.THAI, Language.THAI)
        result =  Translate.execute(source, Language.ENGLISH, Language.LATVIAN);
             }
         else
            {
                int offset = 0;
                while (offset + border < source.length())
                 {
                    result += (Translate.execute(source.substring(offset,offset + border), Language.ENGLISH, Language.LATVIAN));
                    offset += border;
                 }
                    result += (Translate.execute(source.substring(offset,source.length()), Language.ENGLISH, Language.LATVIAN));

             }

    }



return result;
        }
}
